//
//  XLDoctorProfileViewController.m
//  Mine
//
//  Created by 于鹏 on 2017/8/30.
//  Copyright © 2017年 YIVIEW. All rights reserved.
//

#import "XLDoctorProfileViewController.h"

#import "XLProfileInputCellModel.h"
#import "XLProfileTextCellModel.h"
#import "XLProfileTextCell.h"
#import "XLUserInfoModel.h"
#import "YYModel.h"
#import "UIViewController+DismissKeyboard.h"

@interface XLDoctorProfileViewController ()<XLTableViewMainControllerDelegate>
@property (nonatomic, strong)NSMutableArray *textArr;
@end

@implementation XLDoctorProfileViewController
{
    NSString *_engageInStr;//从事
    NSString *_professionalStr;//擅长
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"个人简介";
    XLProfileInputCellModel *section0 = [[XLProfileInputCellModel alloc]initWithPlaceholdText:@"从事:"inputBlock:^(NSString *text) {
        XLLog(@"从事:%@",text);
        _engageInStr = text;
    }];
    XLProfileInputCellModel *section1 = [[XLProfileInputCellModel alloc]initWithPlaceholdText:@"擅长:" inputBlock:^(NSString *text) {
        XLLog(@"擅长:%@",text);
        _professionalStr = text;
    }];
    
    [self.xl_dataSource addObject:@[section0]];
    [self.xl_dataSource addObject:@[section1]];
    self.mainDelegate = self;
    self.tableHeaderView = [self defaultTableHeader];

    [self setupRightBarItemWithTitle:@"上传" action:@selector(uploadAction:)];
    [self setupForDismissKeyboard];
}



#pragma mark - XLTableViewMainControllerDelegate
- (void)xl_mainTableView:(UITableView *)tableView mainTableViewCell:(XLBaseTableViewCell *)mainCell mainCellModel:(XLBaseCellModel *)mainModel sectionArr:(NSArray *)sectionArr atIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 2) {
        XLProfileTextCell *cell = (XLProfileTextCell *)mainCell;
        if (indexPath.row == 0) {
            cell.roundCornerType = XLPRoundCornerCellTypeTop;

        }else if(indexPath.row == sectionArr.count - 1) {
            cell.roundCornerType = XLPRoundCornerCellTypeBottom;
        } else {
            cell.roundCornerType = XLPRoundCornerCellTypeDefault;

        }
    }
}

#pragma mark - Override
- (void)initData {
    [super initData];
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    [parameters setValue:UserInfo.userID forKey:@"doctorID"];
    [parameters setValue:UserInfo.tokenCode forKey:@"token"];
    [XLHTTPRequest UM_getUserInfoWithParameters:parameters success:^(id responseObject) {
        XLUserInfoModel *model = [XLUserInfoModel yy_modelWithDictionary:responseObject[@"data"]];

        NSString *nameStr = [NSString stringWithFormat:@"%@   %@",ISNILORNULL(model.userName),ISNILORNULL(model.gender)];
        
        NSString *hospitalStr;
        if ([ISNILORNULL(model.doctorDepartment) isEqualToString:@""] && [ISNILORNULL(model.doctorPost) isEqualToString:@""]) {
            hospitalStr = [NSString stringWithFormat:@"%@",ISNILORNULL(model.hospitalName)];
        } else {
            hospitalStr = [NSString stringWithFormat:@"%@%@%@%@%@",ISNILORNULL(model.hospitalName),@" | ",ISNILORNULL(model.doctorDepartment),@" ",ISNILORNULL(model.doctorPost)];
        }
        
        NSMutableArray *titleArr = [NSMutableArray array];
        NSMutableArray *detailTextArr = [NSMutableArray array];
        
        [titleArr addObject:@"个人简介:"];
        [detailTextArr addObject:@""];
        
        [titleArr addObject:nameStr];
        [detailTextArr addObject:@""];
        
        if (![ISNILORNULL(model.doctorTitle) isEqualToString:@""]) {
            [titleArr addObject:ISNILORNULL(model.doctorTitle)];
            [detailTextArr addObject:@""];
        }
        [titleArr addObject:hospitalStr];
        [detailTextArr addObject:@""];
        
        [titleArr addObject:@"备注:"];
        [detailTextArr addObject:ISNILORNULL(model.doctorRemark)];
        
        [titleArr addObject:@"擅长:"];
        [detailTextArr addObject:ISNILORNULL(model.doctorProfessional)];
        
        for (int i = 0; i<titleArr.count; i++) {
            XLProfileTextCellModel *model = [XLProfileTextCellModel modelWithTitle:titleArr[i] detailText:detailTextArr[i]];
            if (i == 0) {
                model.isTopGrid = YES;
            }
            [self.textArr addObject:model];
        }
        [self.xl_dataSource addObject:self.textArr];

        dispatch_async(dispatch_get_main_queue(), ^{
            [self.tableView reloadData];
        });
        
    } failure:^(NSError *error, NSString *info) {
        if (error == nil) {
            [self exitLogin:info];
        }
    }];
}

#pragma mark - Action
- (void)uploadAction:(id)sender {
    if (_engageInStr.length == 0 && _professionalStr.length == 0) {
        [self showInfoWithStatus:@"请输入内容"];
        return;
    }
    [self.view endEditing:YES];
    self.rightBarButtonItemEnable = NO;
    [self showWithStatus:@"正在上传"];
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    [parameters setValue:UserInfo.userID forKey:@"doctorID"];
    [parameters setValue:UserInfo.tokenCode forKey:@"token"];
    [parameters setValue:ISNILORNULL(_engageInStr) forKey:@"doctorEngageIn"];
    [parameters setValue:ISNILORNULL(_professionalStr) forKey:@"doctorProfessional"];
    [XLHTTPRequest UM_UploadProfileWithParameters:parameters success:^(id responseObject) {
        self.rightBarButtonItemEnable = YES;
        [self showSuccessWithStatus:@"上传成功"];
        [self.navigationController popViewControllerAnimated:YES];
    } failure:^(NSError *error, NSString *info) {
        self.rightBarButtonItemEnable = YES;
        if (error == nil) {
            [self exitLogin:info];
        }
    }];
}

#pragma mark - Getter
- (NSMutableArray *)textArr {
    if (!_textArr) {
        _textArr = [NSMutableArray array];
    }
    return _textArr;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
